package ru.ifmo.ipimenov.ewf_svn.standalone.controller;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.admin.ISVNTreeHandler;
import org.tmatesoft.svn.core.wc.admin.SVNAdminPath;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.ISVNDirEntryHandler;
import org.tmatesoft.svn.core.SVNDirEntry;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;

import ru.ifmo.ipimenov.ewf_svn.standalone.model.UserProvider;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.ItemProvider;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Model;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Item;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class AcquireAction implements ActionListener {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(AcquireAction.class);

    private JList list;

    public AcquireAction(JList list) {
        this.list = list;
    }

    public void actionPerformed(ActionEvent e) {
        list.setModel(new Model(retrieveData4List()));
    }

//    private List<Item> retrieveData4List(){
//        SVNClientManager clientManager = SVNClientManager.newInstance();
//        SVNLogClient client = clientManager.getLogClient();
//        DAVRepositoryFactory.setup();
//        SVNRepositoryFactoryImpl.setup();
//        FSRepositoryFactory.setup();
//        SVNURL url = null;
//        try {
//            url = SVNURL.parseURIEncoded(UserProvider.getUrls()[0]);
//        } catch (SVNException e) {
//            log.error("Failed to resolve provided url");
//        }
//        PathHandler h = new PathHandler();
//        try {
//            client.doList(url, SVNRevision.HEAD, SVNRevision.HEAD, false, false, h);
//        } catch (SVNException e) {
//            log.error("Failed to process svn request." + e);
//        }
//        return h.getList();
//    }

    private List<Item> retrieveData4List() {
        return ItemProvider.getInstance().loadItems(UserProvider.getInstance().getUser());
    }

    class PathHandler implements ISVNDirEntryHandler {

        List<Item> l = new ArrayList<Item>();

        public List<Item> getList() {
            return l;
        }

        public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {
            Item i = new Item(dirEntry.getName());
            i.setUrl(dirEntry.getURL());
            l.add(i);
        }
    }
}
